#pragma once

#include <iostream>
#include <cstring>
#include <pthread.h>
#include "lock.hpp"

using namespace MyMutex;

namespace MyCond
{
    class cond
    {
    public:
        cond()
        {
            int n = pthread_cond_init(&_cond, nullptr);
            if(n != 0){
                std::cerr << "init cond error" << strerror(n) << std::endl;
            }
        }

        void Wait(mutex& mutex)
        {
            int n = pthread_cond_wait(&_cond, mutex.Get());
            if(n != 0){
                std::cerr << "wait cond error" << strerror(n) << std::endl;
            }
        }

        void signal()
        {
            int n = pthread_cond_signal(&_cond);
            if(n != 0){
                std::cerr << "signal cond error" << strerror(n) << std::endl;
            }
        }

        void broadcast()
        {
            int n = pthread_cond_broadcast(&_cond);
            if(n != 0){
                std::cerr << "broadcast cond error" << strerror(n) << std::endl;
            }
        }

        ~cond()
        {
            int n = pthread_cond_destroy(&_cond);
            if(n != 0){
                std::cerr << "destroy cond error" << strerror(n) << std::endl;
            }
        }
    private:
        pthread_cond_t _cond;
    };
}